//
//  DXWelcomeVC.swift
//  weiBoOfSwift
//
//  Created by simon on 15/12/9.
//  Copyright © 2015年 simon. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
import SVProgressHUD
class DXWelcomeVC: UIViewController {

    lazy var backImgView:UIImageView = {
        
        let backImgView = UIImageView()
         backImgView.image = UIImage(named: "ad_background")

        return backImgView
    }()
   
//    懒加载iconView
    lazy var iconView:UIImageView = {
    let iconView = UIImageView(image: UIImage(named: "avatar_default"))
        iconView.cornerRadius = 45
    return iconView
    }()
//    懒加载messageLabel
    lazy var messageLabel:UILabel = {
     let  messageLabel = UILabel()
        messageLabel.alpha = 0
     messageLabel.text = "欢迎回来"
     messageLabel.font = UIFont.systemFontOfSize(18)
     messageLabel.textColor = UIColor.darkGrayColor()
    
        return messageLabel
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.whiteColor()
        view.addSubview(backImgView)
        view.addSubview(iconView)
        view.addSubview(messageLabel)
          setUpUI()
    }

//    开始动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
          }
    // MARK: - 开始动画
    private func startAnimation(){
        let iconUrlString = DXUserAccountViewModel.shareUserAccount.userAccount!.avatar_large
//    设置头像
        iconView.sd_setImageWithURL( NSURL(string: iconUrlString!), placeholderImage: UIImage(named: "avatar_default_big"))
        
        iconView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(backImgView).offset(100)
        }
        
        UIView.animateWithDuration( 1,
            delay: 0,
            usingSpringWithDamping: 0.6,
            initialSpringVelocity: 0,
            options: [],
            animations: { () -> Void in
                
            self.view.layoutIfNeeded()
            
            }) { (complete) -> Void in
                UIView.animateWithDuration(1,
                    animations: { () -> Void in
                        
                    self.messageLabel.alpha = 1
                        
                    }, completion: { (complete) -> Void in
                        
                        if complete {
            self.performSelector("postNotification", withObject: nil, afterDelay: 0.5)
 //                       动画完成,发通知

                        }
                })
        }
    }
  @objc  private func postNotification(){
                    NSNotificationCenter.defaultCenter().postNotificationName(KChangeRootController, object: self)
    
    }
    
    // MARK: - 设置UI
    private func setUpUI(){
//        设置背景图片的约束
    backImgView.snp_makeConstraints { (make) -> Void in
        make.top.equalTo(view)
        make.bottom.equalTo(view)
        make.leading.equalTo(view)
        make.trailing.equalTo(view)
        }
//        设置头像图片的约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(backImgView)
            make.top.equalTo(backImgView).offset(300)
            make.width.equalTo(90)
            make.height.equalTo(90)
        }
//        设置message约束
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(15)
            make.centerX.equalTo(iconView)
        }
    }
}

























